%different means for control server distribution
alphas = 0.25:0.25:5;
%number of simulations to perform
simulations = size(alphas, 2);

means = zeros(simulations);

for currentSimulation = 1:1:simulations

%time constant
timeConstant = 60;

%Mean value for processing servers
meanServer = 2 * timeConstant;

%Mean value for control Server
meanControl = alphas(currentSimulation) * timeConstant;

%Mean value for client arrival
meanClient = 1 * timeConstant;

%Processing servers queue
processQueue = 0;

%Control queue
controlQueue = 0;

%processing servers status - procesStatus > 0 => bussy, 0 => free
processStatus = zeros(1, 3);
processPrevStatus = ones(1, 3);
processBusy = zeros(1,3);

%control server status
controlStatus = 0;
controlPrevStatus = 1;
controlBusy = 0;
%client Arribal status
nextArrival = exprnd(meanClient);

%current period
t = 0;

%total clients that entered by the system
clientEntryCount = 0;

%total clients that were succesfully served by the system
clientServedCount = 0;

%vector of client entry times
clientEntryTimes = zeros(1100, 1);

%sum of client waiting times
clientWaitingSum = 0;


%simulation
while clientServedCount <= 1000;
	
	
	%if its time for an arrival of a client
	if nextArrival <= 0
		%printf("\n Arrival, t = %d", t);
		%increase client count
		clientEntryCount = clientEntryCount + 1;
		%store the client entry time
		clientEntryTimes(clientEntryCount) = t;
		%add it to the processing queue
		processQueue = processQueue + 1;
		%schedule next arrival
		nextArrival = exprnd(meanClient);
	else
		nextArrival = nextArrival - 1;
	end
	
	%if processServers are free, add work
	for i = 1:1:3
		if processStatus(i) <= 0
			if processBusy(i) == 1 && processPrevStatus(i) > 0
				processPrevStatus(i) = processStatus(i);
				%printf("\n Process ready, to control SRV %d t %d ", i, t);
				%pass the client to control server
				controlQueue = controlQueue + 1;
			end
			%if there are clients waiting, take one
			if processQueue > 0
				processQueue = processQueue - 1;
				processStatus(i) = exprnd(meanServer);
				processBusy(i) = 1;
			end
		%if its busy, add tick
		else
			processPrevStatus(i) = processStatus(i);
			processStatus(i) = processStatus(i) - 1;
		end
	end
	
	%similar process for control server
	if controlStatus <= 0
		if controlBusy == 1  && controlPrevStatus > 0
			controlPrevStatus = controlStatus;
			%printf("\n Client Released t = %d", t);
			%client released
			%increase client served count
			clientServedCount = clientServedCount + 1;
			%add the time the client was in the system to the clients time sum 
			timeElapsed = t - clientEntryTimes(clientServedCount);
			clientWaitingSum = clientWaitingSum + timeElapsed;
			clientTotalTimes(clientServedCount) = timeElapsed;
		end
		%if there are clients waiting, take one
		if controlQueue > 0
			controlQueue = controlQueue - 1;
			controlStatus = exprnd(meanControl);
			controlBusy = 1;
		end
	%if its busy, add tick
	else
		controlPrevStatus = controlStatus;
		controlStatus = controlStatus - 1;
	end
	
	t = t + 1;
end

meanTimeInQueue = clientWaitingSum / (clientServedCount * timeConstant)
means(currentSimulation) = meanTimeInQueue;

%TODO: print/plot results
end

figure;
plot(alphas, means);
%title('Tiempo medio en cola por cliente');
xlabel('Alpha');
ylabel('Tiempo medio en cola');

